package com.dayuanit.booksystem.v4.service.impl;

import com.dayuanit.booksystem.v4.domain.BookInfo;
import com.dayuanit.booksystem.v4.entity.Book;
import com.dayuanit.booksystem.v4.exception.BizException;
import com.dayuanit.booksystem.v4.mapper.BookMapper;
import com.dayuanit.booksystem.v4.service.BookService;
import com.dayuanit.booksystem.v4.util.MybatisUtil;
import com.dayuanit.booksystem.v4.util.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Arrays;
import java.util.List;

public class BookServiceImpl implements BookService {
    @Override
    public void addBook(String bookName, String author, String price, long publisherId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            int rows = mapper.insert(price, author, bookName, publisherId);
            if (1 != rows) {
                throw new BizException("操作失败");
            }
        }
    }

    @Override
    public Book queryBook(long bookId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getBookById(bookId);
        }
    }

    @Override
    public Book queryBook(String bookName) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getBookByName(bookName);
        }
    }

    @Override
    public List<Book> listBook(long publisherId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            List<Book> books = mapper.listBook(publisherId);
            return books;
        }
    }

    @Override
    public int countBook(long publisherId) {
        return 0;
    }

    @Override
    public void upateBookPrice(long bookId, String newPrice) {

    }

    @Override
    public void updateBookPrice(String bookName, String newPrice) {

    }

    @Override
    public BookInfo queryBookInfo(long bookId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            BookInfo bookInfo = mapper.getBookInfo(bookId);
            return bookInfo;
        }
    }

    @Override
    public int countBookInfo(String author, String bookName, Long publisherId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);

            bookName = StringUtils.isBlank(bookName) ? null : bookName;
            author = StringUtils.isBlank(author) ? null : author;

            return mapper.countBookInfo(bookName, author, publisherId);
        }
    }

    @Override
    public List<BookInfo> listBookInfo(String author, String bookName, Long publisherId, int currentPage) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);

            bookName = StringUtils.isBlank(bookName) ? null : bookName;
            author = StringUtils.isBlank(author) ? null : author;

            int pageSize = 2;

            //offset = (currentPage - 1) * 每页显示的个数
            int offset = (currentPage - 1) * pageSize;

            List<BookInfo> bookInfos = mapper.listBookInfo(bookName, author, publisherId, offset, pageSize);
            return bookInfos;
        }
    }

    @Override
    public List<Book> listBook(Long... publisherId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            //面试题：数组转集合  & 集合转数组 查资料
            List<Long> publisherList = Arrays.asList(publisherId);
            List<Book> books = mapper.listBookByPubliserId(publisherList);
            return books;
        }
    }
}
